package org.compiere.process;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MRefList;
import org.compiere.model.Mhrmmaterialaid;
import org.compiere.model.Mhrmmaterialaidline;
import org.compiere.model.Mhrmsettax;
import org.compiere.model.Mhrmtaxexemptions;
import org.compiere.model.X_hrm_materialaid;
import org.compiere.model.X_hrm_set_tax;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.eevolution.model.X_HR_Employee;
import extend.org.compiere.hrm.TaxCompute;
import extend.org.compiere.hrm.TaxVariable;
import extend.org.compiere.hrm.TypePayment;

public class GenerateMaterialAid extends SvrProcess {
	
	/** Zero status  */	
	double SUM = .0;
	private int C_BPartner_ID = 0;
	private Timestamp dateFrom = null;
	private BigDecimal MZP8 = new BigDecimal(0);
	
	
	/**  */
	private Mhrmmaterialaid maid = null;
	private Mhrmmaterialaidline maidline = null;
	private TaxVariable taxcomp = null;
	
	/** */
	String closeTax = "N";

	@Override
	protected void prepare() {
		
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
				String name = para[i].getParameterName();
				if (name.equals("CloseTax"))
					closeTax = para[i].getParameter().toString();
				else
					log.log(Level.INFO, "Unknown Parameter: " + name);			
		}
		
		maid = new Mhrmmaterialaid(getCtx(), getRecord_ID(), null);
	}

	@Override
	protected String doIt() throws Exception {
	
		/** Set Tax parameters*/
		dateFrom = maid.getDateFrom();
		BigDecimal taxMZP = Mhrmsettax.getRateValue(X_hrm_set_tax.HRM_TAXTYPE_MZP, dateFrom);
		
		/** Get Partner_ID */
		C_BPartner_ID = maid.getC_BPartner_ID();
		X_HR_Employee he = X_HR_Employee.getHR_Employee(C_BPartner_ID, true);
		//if (he.getC_BPartner_ID() == 0) throw new AdempiereException ("Info about employee was empty");
		
		maidline = Mhrmmaterialaidline.getAidRec(C_BPartner_ID, dateFrom);
		if (maidline == null)
			maidline = new Mhrmmaterialaidline(getCtx(), 0, null);
		maidline.sethrm_materialaid_ID(maid.gethrm_materialaid_ID());
		maidline.setC_BPartner_ID(C_BPartner_ID);
		maidline.setHR_Department_ID(he.getHR_Department_ID());
		maidline.setHR_Job_ID(he.getHR_Job_ID());
		maidline.setDateFrom(dateFrom);
		maidline.setName(MRefList.getListName(getCtx(), X_hrm_materialaid.HRM_MATERIALAIDTYPE_AD_Reference_ID, maid.gethrm_materialaidtype()));
		SUM = maid.gethrm_total().doubleValue();
		if (SUM <= 0)
			throw new AdempiereException ("Info about SUM was empty.");
		
		BigDecimal rate = Mhrmtaxexemptions.getRate(maid.gethrm_materialaidtype(), dateFrom);
		if (rate.doubleValue() > 0){
			MZP8 = taxMZP.multiply(rate);
			if (SUM < MZP8.doubleValue()){
	    		MZP8 = maid.gethrm_total();
	    		SUM = .0;
			}else{				
				SUM = SUM - MZP8.doubleValue();				 
			}
		}else{			
			MZP8 = new BigDecimal(0);
			SUM = maid.gethrm_total().doubleValue();
		}
		
		/** Generate employee tax */
		taxcomp =  TaxCompute.GetTax(he.getC_BPartner_ID(), SUM, dateFrom, TypePayment.MATERIALAID);
						
	    maidline.sethrm_OPV(new BigDecimal(taxcomp.getOPV()));
	    maidline.sethrm_IPN(new BigDecimal(taxcomp.getIPN()));
	    maidline.sethrm_CNalog(new BigDecimal(taxcomp.getCNALOG()));
	    maidline.sethrm_COtch(new BigDecimal(taxcomp.getCOTCH()));
	    maidline.sethrm_amountsalary(MZP8.add(new BigDecimal(taxcomp.getTOTAL() + taxcomp.getOPV() + taxcomp.getIPN())));
	    maidline.sethrm_total(new BigDecimal(taxcomp.getTOTAL() + taxcomp.getOPV() + taxcomp.getIPN()));
	    maidline.sethrm_amount(MZP8);
	    	    
	    if (closeTax.equals("Y")){
	    	maidline.setProcessed(true);
	    	maid.setProcessed(true);	  
	    	maid.saveEx();
	    }	    
	    maidline.saveEx();
	   
	    
	    return Msg.translate(Env.getCtx(), "Success");
	}

}
